import UIKit
//导入SnapKit命名空间
import SnapKit
//定义协议
@objc protocol VisitorLoginViewDelegate: NSObjectProtocol {
    //声明协议方法
    //登录的协议方法
    
    //声明可选的协议方法 使用 optional关键字(是OC中的关键字)
    optional func userWillLogin()
    /// 注册的协议方法
    func userWillRegister()
}

//MARK:定义协议
class VisitorLoginView: UIView {

    //必须声明一个遵守协议的代理对象  为了防止循环引用最好使用weak
    weak var visitorLoginViewDelegate: VisitorLoginViewDelegate?
    override init(frame: CGRect) {
        super.init(frame: frame)
        //设置UI
        setupUI()
    }
    //默认实现报错
    //当程序员通过 sb / xib 加载该视图 程序就会崩溃
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //MARK:设置页面的信息
     func setupInfoWith(tiptext: String,imageName: String?){
        tiplabel.text = tiptext
        if imageName != nil {
            //不是首页
            circleIcon.image = UIImage(named: imageName!)
            largeHouse.hidden = true
            bringSubviewToFront(circleIcon)
        }else{
            //是首页
            startAnimation()
        }
    }
    //点击事件的监听方法
    @objc private func registerBtnDidClick() {
        visitorLoginViewDelegate?.userWillRegister()
    }
    @objc private func loginBtnDidClick() {
        visitorLoginViewDelegate?.userWillLogin!()
    }
    private func setupUI() {
        //添加子视图
        addSubview(circleIcon)
        /**
        确保遮挡图只能遮挡的住圆圈,遮不住大房子
        */
        addSubview(backView)
        addSubview(largeHouse)
        addSubview(tiplabel)
        addSubview(loginbtn)
        addSubview(register)
        circleIcon.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
        }
        largeHouse.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleIcon.snp_center)
        }
        tiplabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(circleIcon.snp_centerX)
            make.top.equalTo(circleIcon.snp_bottom).offset(16)
            make.width.equalTo(225)
            make.height.equalTo(40)
        }
        loginbtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(tiplabel.snp_left)
            make.top.equalTo(tiplabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        register.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(tiplabel.snp_right)
            make.top.equalTo(tiplabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        backView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleIcon.snp_center)
        }
        
       self.backgroundColor = UIColor(white: 0.93, alpha: 1)
        //添加点击事件
        loginbtn.addTarget(self, action: "loginBtnDidClick", forControlEvents: .TouchUpInside)
        register.addTarget(self, action: "registerBtnDidClick", forControlEvents: .TouchUpInside)
    }
    //懒加载所有的视图
    //圈圈
    lazy var circleIcon : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //大房子
    lazy var largeHouse : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //提示文案
    lazy var tiplabel : UILabel = {
        let l = UILabel()
        l.font = UIFont.systemFontOfSize(14)
        l.text = "登录后,别人评论你的微博登录后,别人评论你的微博"
        l.numberOfLines = 2
        l.textColor = UIColor.darkGrayColor()
        l.textAlignment = .Center
        
        l.sizeToFit()
        return l
    }()
    //登录按钮
    lazy var loginbtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("登录", forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(16)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        return btn
    }()
    //注册按钮
    lazy var register: UIButton = {
        let btn = UIButton()
        btn.setTitle("注册", forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(16)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        return btn
    }()
    //遮挡图片
    private lazy var backView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    private func startAnimation () {
        let ani = CABasicAnimation(keyPath: "transform.rotation")
        //重复次数
        ani.repeatCount = MAXFLOAT
        ani.toValue = 2 * M_PI
        //设置动画时间
        ani.duration = 20
        //当动画完成或处于非活跃状态 动画效果不移除图层
        ani.removedOnCompletion = false
        //基础动画添加到图层
        circleIcon.layer.addAnimation(ani, forKey: nil)
    }
}
